/**
 * 
 */
package com.study.timer.quartz;

import java.util.Date;

import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

/**
 * @author Administrator
 * @date 2019年4月27日
 */
public class QuartzTest {

	public static void main(String[] args) throws SchedulerException{
		
		 String requestedFile = System.getProperty("org.quartz.properties");
		 System.out.println(requestedFile);
		//1.创建Scheduler的工厂
		StdSchedulerFactory stdSchedulerFactory = new StdSchedulerFactory();
		//2.从工厂中获取调度器实例
		Scheduler scheduler = stdSchedulerFactory.getScheduler();
		
		//3.创建JobDetail
		JobDetail build = JobBuilder.newJob(MyJob.class)
			.withDescription("this is a ram job")
			.withIdentity("ramjob","ramGroup") //job 的name和group
			.build();
		
		//任务运行的时间，SimpleSchedle类型触发器有效
		long time = System.currentTimeMillis() + 3000L;  //3s 后启动任务
		Date date = new Date(time);
		
		//4.创建Trigger
        //使用SimpleScheduleBuilder或者CronScheduleBuilder
		Trigger tb = (Trigger) TriggerBuilder.newTrigger()
		.withDescription("")
		.withIdentity("ramTrigger", "groupTrigger")
		.startAt(date)
		.withSchedule(CronScheduleBuilder.cronSchedule("0/2 * * * * ?")).build(); //两秒执行一次
		
		//5.注册任务和定时器
		scheduler.scheduleJob(build, tb);
		//6.启动 调度器
		scheduler.start();
	}
}
